from eim.tools.dbtool import DBTool
from eim.tools.searchtool import MatchLogic,PartnerRS,SearchCondition,SearchConditionList
from eim.DataBasModals.mode import t_powers,t_operate,t_mastnetcomlog,t_netanalysisrule
from eim.result.page_result import PageResult
from eim.result.simple_result import SimpleResult
from flask import current_app
from sqlalchemy import func,select,update,delete,text,or_,and_,insert,join
from sqlalchemy.engine import LegacyRow,Connection,CursorResult

class NetAnalysisRule:
    """
    日志解析规则表
    """
    def __init__(self,db):
        self.db = db
    
    def appendNewRule(self,vals:dict)->int:
        """
        增加新的规则

        Args:
            vals (dict): 新规则列参数

        Returns:
            int: 新增的id
        """
        try:
            if(not isinstance(vals,dict)):
                raise Exception("新规则参数必须为jason格式")
            con:Connection = self.db.engine.connect()
            qry = insert(t_netanalysisrule).values(vals)
            res:CursorResult = con.execute(qry)
            return res.inserted_primary_key[0]
        except Exception as es:
            raise es
        
    def getRunRule(self)->list:
        """
        获取在运行的日志分析规则

        Returns:
            list: 日志分析规则列表
        """
        try:
            result=[]
            qry=select(t_netanalysisrule)
            qry=qry.where(t_netanalysisrule.c.status=='01')
            con = self.db.engine.connect()
            res:CursorResult = con.execute(qry)
            for row in res.fetchall():
                result.append(row._asdict())
            return result
        except Exception as ex:
            raise ex
